import axios from 'axios'
// import router from '~/router/index'
// import store from '../store'
// import Config from '~/settings'
import cryptoJs from 'crypto-js'
// import Cookies from 'js-cookie'
import {guid,key,iv} from './common'

// 创建axios实例
const service = axios.create({
    baseURL: process.env.NODE_ENV === 'production' ? process.env.BASE_URL : '/', // api 的 base_url
    timeout: 600000 // 请求超时时间
})

//拦截器给所有请求参数加密
service.interceptors.request.use(

    config => {
        if (config.url.indexOf("upload") > 0) {
            return config
        }
        config.data.uuid = guid();
        config.data.platform = 3;
        let encData = cryptoJs.AES.encrypt(JSON.stringify(config.data), key, {
            iv: iv,
            mode: cryptoJs.mode.CBC,
            padding: cryptoJs.pad.Pkcs7
        }).toString();
        console.log(JSON.stringify(config.data),config.url);
        config.data = {"secword": encData};
        return config
    },
    err => {
        return Promise.reject(err)
    }
)

// response 拦截器
/*service.interceptors.response.use(
    response => {
        const code = response.status
        if (code < 200 || code > 300) {
            this.$notification['error']({
                message: response.message
            })
            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    error => {
        let code = 0
        try {
            code = error.response.data.status
        } catch (e) {
            if (error.toString().indexOf('Error: timeout') !== -1) {
                this.$notification['error']({
                    message: '网络请求超时'
                })
                return Promise.reject(error)
            }
        }
        if (code) {
            if (code === 401) {
                store.dispatch('LogOut').then(() => {
                    // 用户登录界面提示
                    Cookies.set('point', 401)
                    location.reload()
                })
            } else if (code === 403) {
                router.push({path: '/401'})
            } else {
                const errorMsg = error.response.data.message
                if (errorMsg !== undefined) {
                    this.$notification['error']({
                        message: errorMsg
                    })
                }
            }
        } else {
            this.$notification['error']({
                message: '接口请求失败'
            })
        }
        return Promise.reject(error)
    }
)*/
export default service
